package im.elephas.androfan.activity;

import im.elephas.androfan.R;
import im.elephas.androfan.common.widget.UserWidget;
import im.elephas.androfan.fanfouapi.events.IUsersEvent;
import im.elephas.androfan.fanfouapi.items.UserItem;

import java.util.List;

import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.LinearLayout;

public abstract class UserActivity extends AndroFanActivity {

	private List<UserItem> friendsListUserItems;

	private LinearLayout linearLayout;

	protected IUsersEvent mIUserEvent = new IUsersEvent() {
		@Override
		public void usersHandler(List<UserItem> lui) {
			friendsListUserItems = lui;
			getActivity().runOnUiThread(new Runnable() {

				@Override
				public void run() {
					refreshFriends();
				}
			});
		}
	};

	private void refreshFriends() {
		setContentView(R.layout.friendslist);

		linearLayout = (LinearLayout) findViewById(R.id.friendsLinearLayout);

		boolean colorDark = false;
		for (UserItem item : friendsListUserItems) {
			UserWidget uw = new UserWidget(linearLayout, getActivity(), item);

			if (colorDark)
				uw.getWidgetView().setBackgroundColor(
						Color.argb(255, 202, 222, 235));
			else
				uw.getWidgetView().setBackgroundColor(
						Color.argb(255, 237, 237, 237));
			colorDark = !colorDark;

			uw.addToParentView();
		}
	}

	protected abstract void getUsers();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.statusesfreshing);

		setTitle();
		getUsers();
	}

	protected abstract void setTitle();

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		refreshFriends();
	}
}
